package rigo.ricard.pris.rs.ac.uns.dmi.top.advertisings.bean;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.model.map.LatLng;

import rigo.ricard.pris.rs.ac.uns.dmi.geocode.GeocodeByLocation;
import rigo.ricard.pris.rs.ac.uns.dmi.model.Advertising;
import rigo.ricard.pris.rs.ac.uns.dmi.service.AdvertisingLocalServiceUtil;
import rigo.ricard.pris.rs.ac.uns.dmi.util.CurrentUser;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
import com.liferay.util.mail.MailEngine;

@SuppressWarnings("serial")
@ManagedBean(name = "topAdsBean")
@ViewScoped
public class TopAdsBean implements Serializable {
	
	private static final Log log = LogFactoryUtil.getLog(TopAdsBean.class);

	private List<Advertising> advertisings = null;
	
	private Advertising selectedAd = null;
	
	private String subject = null;
	private String message = null;
	
	private long selectedUserId = 0; 
	private String selectedLocation = null; 
	private LatLng cordinate = null;
	
	public TopAdsBean() {
		//TODO just for user
		init();
	}
	
	public void initMessage() {
		subject = null;
		message = null;
	}
	
	public void init() {
		try {
			advertisings = AdvertisingLocalServiceUtil.getTopAdverisigns();
		} catch (SystemException e) {
			log.error("error loading advertisings : "+e.getMessage());
		}
	}

	public boolean isUserLogedIn() {
		return CurrentUser.getCurrentUser(FacesContext.getCurrentInstance())!=null;
	}
	
	public void sendMessage() {
		try {
			User user = UserLocalServiceUtil.getUser(selectedUserId);
			User currentUser = CurrentUser.getCurrentUser(FacesContext.getCurrentInstance());
			if(user!=null && currentUser!=null) {
				MailEngine.send(currentUser.getEmailAddress(), user.getEmailAddress(), subject, message);
			} else {
				throw new Exception();
			}
			initMessage();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Mail sent", ""));
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error occured", ""));
		}
		RequestContext.getCurrentInstance().execute("emailDlg.hide()");
	}

	public String getManufacturerString(Advertising advertising) {
		try {
			return AssetCategoryLocalServiceUtil.getAssetCategory(advertising.getCarManufacturerId()).getName();
		} catch (Exception e) {
			return " - ";
		}
	}
	
	public String getModelString(Advertising advertising) {
		try {
			return AssetCategoryLocalServiceUtil.getAssetCategory(advertising.getCarModelId()).getName();
		} catch (Exception e) {
			return " - ";
		}
	}

	public List<Advertising> getAdvertisings() {
		return advertisings;
	}

	public void setAdvertisings(List<Advertising> advertisings) {
		this.advertisings = advertisings;
	}

	public Advertising getSelectedAd() {
		return selectedAd;
	}

	public void setSelectedAd(Advertising selectedAd) {
		this.selectedAd = selectedAd;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public long getSelectedUserId() {
		return selectedUserId;
	}

	public void setSelectedUserId(long selectedUserId) {
		initMessage();
		this.selectedUserId = selectedUserId;
		RequestContext.getCurrentInstance().execute("emailDlg.show()");
	}

	public String getSelectedLocation() {
		return selectedLocation;
	}

	public void setSelectedLocation(String selectedLocation) {
		this.selectedLocation = selectedLocation;
		cordinate = GeocodeByLocation.getCordinatesByLocation(selectedLocation);
		RequestContext.getCurrentInstance().execute("gmapDialog.show()");
	}
	
	public boolean isGoogleMapRendered() {
		return cordinate!=null;
	}

	public LatLng getCordinate() {
		return cordinate;
	}

	public void setCordinate(LatLng cordinate) {
		this.cordinate = cordinate;
	}
	
	
	
}
